Fix ImageGeneratingChatClient duplicating preceding content and dropping following content#7622
Merged
Merged
Conversation
…dd regression tests
Copilot
AI
changed the title
[WIP] Fix duplication and content drop in ImageGeneratingChatClient
Fix ImageGeneratingChatClient duplicating preceding content and dropping following content
Jul 13, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a content-rewrite bug in ImageGeneratingChatClient where reverse iteration could duplicate earlier content items and drop later items when replacing image-generation function content.
Changes:
- Updated
ReplaceImageGenerationFunctionResultsto iterate forward so surrounding content is preserved and not duplicated/dropped. - Added regression tests for both non-streaming and streaming responses to validate content order and cardinality when image-generation content appears at the start/middle/end.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/ImageGeneratingChatClient.cs | Switches replacement logic to forward iteration to preserve content order and prevent duplication/dropping. |
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/ImageGeneratingChatClientTests.cs | Adds regression coverage for surrounding-content preservation in both GetResponseAsync and GetStreamingResponseAsync. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
jozkee
approved these changes
Jul 13, 2026
jeffhandley
approved these changes
Jul 13, 2026
Member
|
/backport to release/10.8 |
Contributor
|
Started backporting to release/10.8: https://github.com/dotnet/extensions/actions/runs/29286341323 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ReplaceImageGenerationFunctionResultsiterated in reverse, causing content items after an image-generation match to be silently dropped (visited whilenewContentswas null) and items before it to be duplicated (CopyListcaptured the prefix, then the reverse loop re-appended those same items).For a message with contents
[A, ImageGenFunctionCall, B], the old code produced[A, ImageGenerationToolCallContent, A]— droppingBand duplicatingA.Changes
ImageGeneratingChatClient.cs: ChangedReplaceImageGenerationFunctionResultsfrom reverse to forward iteration.CopyList(contents, i)now correctly captures only items before the replacement point; remaining items are appended naturally as the loop continues.ImageGeneratingChatClientTests.cs: Added regression tests for bothGetResponseAsyncandGetStreamingResponseAsynccovering image-generation content at the start, middle, and end of the content list with surrounding unrelated content items of mixed types (FunctionResultContent,UsageContent).